package com.dg11185.nearshop.baidumap;

import com.android.nearshop.R;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.overlayutil.DrivingRouteOvelray;
import com.baidu.mapapi.overlayutil.TransitRouteOverlay;
import com.baidu.mapapi.overlayutil.WalkingRouteOverlay;
import com.baidu.mapapi.search.route.DrivingRoutePlanOption;
import com.baidu.mapapi.search.route.DrivingRouteResult;
import com.baidu.mapapi.search.route.OnGetRoutePlanResultListener;
import com.baidu.mapapi.search.route.PlanNode;
import com.baidu.mapapi.search.route.RoutePlanSearch;
import com.baidu.mapapi.search.route.TransitRoutePlanOption;
import com.baidu.mapapi.search.route.TransitRouteResult;
import com.baidu.mapapi.search.route.WalkingRoutePlanOption;
import com.baidu.mapapi.search.route.WalkingRouteResult;

/**
 * 通过传入起始坐标点查询线路
 * 
 * @author：Daibo
 * @time：2014-7-10 下午6:19:44
 */
public class MyMapRoute {

    /** 单实例类变量 */
    private static MyMapRoute customMapRoute;

    private LatLng stPoi, enPoi;

    private String cityName;

    private RouteSearchListener myListener;

    /** 保存线路查询结果 */
    private WalkingRouteResult walkingResult = null;

    private TransitRouteResult transitResult = null;

    private DrivingRouteResult drivingResult = null;

    // 搜索相关
    private RoutePlanSearch mSearch = null; // 搜索模块，也可去掉地图模块独立使用

    private MyMapRoute() {

        mSearch = RoutePlanSearch.newInstance();
        mSearch.setOnGetRoutePlanResultListener(listener);
    }

    public static MyMapRoute getInstance() {
        if (customMapRoute == null) {
            customMapRoute = new MyMapRoute();
        }
        return customMapRoute;
    }

    public MyMapRoute setStNode(double[] stPoi) {
        this.stPoi = new LatLng(stPoi[0], stPoi[1]);
        return this;
    }

    public MyMapRoute setEnNode(double[] enPoi) {
        this.enPoi = new LatLng(enPoi[0], enPoi[1]);
        return this;
    }

    public MyMapRoute setCityName(String cityName) {
        this.cityName = cityName;
        return this;
    }

    public MyMapRoute setMyListener(RouteSearchListener myListener) {
        this.myListener = myListener;
        return this;
    }

    /**
     * 获取route的值，用于在其它地方显示
     */
    public WalkingRouteResult getWalkingRouteResult() {
        return walkingResult;
    }

    public DrivingRouteResult getDrivingRouteResult() {
        return drivingResult;
    }

    public TransitRouteResult getTransitRouteResult() {
        return transitResult;
    }

    /**
     * 调用路线查询相关方法
     */
    public void startTransitSearch() {
        mSearch.transitSearch(new TransitRoutePlanOption().city(cityName).from(PlanNode.withLocation(stPoi))
                .to(PlanNode.withLocation(enPoi)));
    }

    public void startDrivingSearch() {
        mSearch.drivingSearch(new DrivingRoutePlanOption().from(PlanNode.withLocation(stPoi)).to(
                PlanNode.withLocation(enPoi)));
    }

    public void startWalkingSearch() {
        mSearch.walkingSearch(new WalkingRoutePlanOption().from(PlanNode.withLocation(stPoi)).to(
                PlanNode.withLocation(enPoi)));
    }

    /**
     * 获取路线后的回调方法
     */
    private OnGetRoutePlanResultListener listener = new OnGetRoutePlanResultListener() {

        @Override
        public void onGetWalkingRouteResult(WalkingRouteResult result) {
            walkingResult = result;
            myListener.onGetWalkingRouteResult(result);
        }

        @Override
        public void onGetTransitRouteResult(TransitRouteResult result) {
            transitResult = result;
            myListener.onGetTransitRouteResult(result);
        }

        @Override
        public void onGetDrivingRouteResult(DrivingRouteResult result) {
            drivingResult = result;
            myListener.onGetDrivingRouteResult(result);
        }
    };

    /***
     * 
     * 调用该方法直接在地图上显示overlay
     */
    public void showTransitRouteOverlay(BaiduMap mBaiduMap, int curPlan) {
    	try {
            MyTransitRouteOverlay overlay = new MyTransitRouteOverlay(mBaiduMap);
            mBaiduMap.setOnMarkerClickListener(overlay);
            overlay.setData(transitResult.getRouteLines().get(curPlan));
            overlay.addToMap();
            MapStatusUpdate update = MapStatusUpdateFactory.newLatLng(stPoi);
            mBaiduMap.setMapStatus(update);
		} catch (Exception e) {
			e.printStackTrace();
		}
//        overlay.zoomToSpan();
    }

    public void showDrivingRouteOverlay(BaiduMap mBaiduMap, int curPlan) {
        MyDrivingRouteOverlay overlay = new MyDrivingRouteOverlay(mBaiduMap);
        mBaiduMap.setOnMarkerClickListener(overlay);
        overlay.setData(drivingResult.getRouteLines().get(curPlan));
        overlay.addToMap();
        MapStatusUpdate update = MapStatusUpdateFactory.newLatLng(stPoi);
        mBaiduMap.setMapStatus(update);
//        overlay.zoomToSpan();
    }

    public void showWalkingRouteOverlay(BaiduMap mBaiduMap, int curPlan) {
        MyWalkingRouteOverlay overlay = new MyWalkingRouteOverlay(mBaiduMap);
        mBaiduMap.setOnMarkerClickListener(overlay);
        overlay.setData(walkingResult.getRouteLines().get(curPlan));
        overlay.addToMap();
        MapStatusUpdate update = MapStatusUpdateFactory.newLatLng(stPoi);
        mBaiduMap.setMapStatus(update);
//        overlay.zoomToSpan();
    }

    // 定制RouteOverly
    private class MyDrivingRouteOverlay extends DrivingRouteOvelray {

        public MyDrivingRouteOverlay(BaiduMap baiduMap) {
            super(baiduMap);
        }

        @Override
        public BitmapDescriptor getStartMarker() {
            return BitmapDescriptorFactory.fromResource(R.drawable.map_route_start);
        }

        @Override
        public BitmapDescriptor getTerminalMarker() {
            return BitmapDescriptorFactory.fromResource(R.drawable.map_route_end);
        }
    }

    private class MyWalkingRouteOverlay extends WalkingRouteOverlay {

        public MyWalkingRouteOverlay(BaiduMap baiduMap) {
            super(baiduMap);
        }

        @Override
        public BitmapDescriptor getStartMarker() {
            return BitmapDescriptorFactory.fromResource(R.drawable.map_route_start);
        }

        @Override
        public BitmapDescriptor getTerminalMarker() {
            return BitmapDescriptorFactory.fromResource(R.drawable.map_route_end);
        }
    }

    private class MyTransitRouteOverlay extends TransitRouteOverlay {

        public MyTransitRouteOverlay(BaiduMap baiduMap) {
            super(baiduMap);
        }

        @Override
        public BitmapDescriptor getStartMarker() {
            return BitmapDescriptorFactory.fromResource(R.drawable.map_route_start);
        }

        @Override
        public BitmapDescriptor getTerminalMarker() {
            return BitmapDescriptorFactory.fromResource(R.drawable.map_route_end);
        }
    }

}

interface RouteSearchListener {

    public void onGetTransitRouteResult(TransitRouteResult result);

    public void onGetDrivingRouteResult(DrivingRouteResult result);

    public void onGetWalkingRouteResult(WalkingRouteResult result);
}